package com.internship.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.internship.model.DepartmentInfo;
import com.internship.page.Page;
import com.internship.service.DepartmentInfoService;


@Controller
@RequestMapping("department")
public class DepartmentController {
	private final Logger logger = Logger.getLogger(DepartmentController.class);
	
	@Autowired
	private DepartmentInfoService departmentInfoService;
	public static HttpSession getSession() {
		HttpSession session = null;
		try {
			session = getRequest().getSession();
		} catch (Exception e) {
		}
		return session;
	}

	public static HttpServletRequest getRequest() {
		ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes();
		return attrs.getRequest();
	}
	private void clearSession() {
		getSession().invalidate();
	}

	@RequestMapping("/department_list.do")
	public ModelAndView getDepartmentList() {
		return getDepartmentList(null,1);
	}
	@RequestMapping("/department_page_list.do")
	public ModelAndView getDepartmentList(@RequestParam String XYMC,@RequestParam int pageNumber) {
		Page<DepartmentInfo> pageList = departmentInfoService.getDepartmentInfoList(XYMC, pageNumber);
		ModelAndView mv = new ModelAndView("department_info");
		mv.addObject("currentPage", pageNumber);
		mv.addObject("XYMC", XYMC);
		mv.addObject("totalPage", pageList.getTotalPage());
		mv.addObject("totalCount", pageList.getTotalCount());
		mv.addObject("departmentList", pageList.getResultList());
		return mv;
	}
}